﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using AcademicPlanner.Domain.Models;

namespace AcademicPlanner.Presentation.Views.Shared
{
	/// <summary>
	/// Possible scenarios of a <c>PersonForm</c> form.
	/// </summary>
	/// <version>1.0</version>
	/// <since>1.0</since>
	public enum PersonFormScenario
	{
		REGISTER,
		COMPLETE_REGISTRATION
	}


	/// <summary>
	/// PersonFormModel represents a model object for a <c>PersonForm</c> form.
	/// </summary>
	/// <version>1.0</version>
	/// <since>1.0</since>
	public class PersonFormModel : FormModel
	{
		/// <summary>
		/// Gets or sets the scenario to use the <c>PersonForm</c> form under.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public PersonFormScenario Scenario { get; set; }

		/// <summary>
		/// Gets or sets the <c>Person</c> whose data to save.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public Person Person { get; set; }
	}


	/// <summary>
	/// PersonForm represents a form to save data about a <c>Person</c>.
	/// This form embeds a <c>AccountsForm</c> form if used under the <c>COMPLETE_REGISTRATION</c>
	/// scenario.
	/// </summary>
	/// <seealso cref="T:SaveAccountsForm"/>
	/// <version>1.0</version>
	/// <since>1.0</since>
	public partial class PersonForm : Form<PersonFormModel>
	{
		/// <summary>
		/// Gets the model object for this form control.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public new PersonFormModel Model { get { return (PersonFormModel)base.Model; } }

		/// <summary>
		/// Gets or sets the model object for the <c>AccountForm</c> form contained in this form.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public AccountFormModel SaveAccountModel { get; set; }

		/// <summary>
		/// Gets or sets the value of the email field of this form.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public string EmailValue { get; set; }

		/// <summary>
		/// Gets or sets the value of the name field of this form.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public string NameValue { get; set; }


		/// <summary>
		/// Handles the load page event. This method initializes or customizes the form's properties
		/// based on the model object's <c>Scenario</c>.
		/// </summary>
		/// <param name="sender">The sender of the event.</param>
		/// <param name="e">The details of the event.</param>
		/// <version>1.0</version>
		/// <since>1.0</since>
		protected void Page_Load(object sender, EventArgs e)
		{
			method = FormMethod.Post;

			if (Model.Scenario.Equals(PersonFormScenario.COMPLETE_REGISTRATION))
			{
				actionName = "Setup";
				controllerName = "People";
				routeValues = new { pPersonId = Model.Person.EntityId };
				Name = "setupPersonForm";
				SubmitButtonValue = Tr("Complete Registration");
				SaveAccountModel = new AccountFormModel();
				SaveAccountModel.IsSub = true;
				SaveAccountModel.NamesPrefix = GetFieldName("Account");
			}
			else
			{
				actionName = "Signup";
				controllerName = "People";
				routeValues = new object { };
				Title = Tr("Register");
				Name = "registerForm";
				SubmitButtonValue = Tr("Register");
			}

			if (string.IsNullOrEmpty(Model.Id)) Model.Id = Name;

			if (Model.Scenario.Equals(PersonFormScenario.COMPLETE_REGISTRATION))
			{
				EmailValue = Model.Person.Email;
				NameValue = Model.Person.Name;
			}
			else
			{
				EmailValue = "";
			}
		}
	}
}